package com.nowadays.mobile.ui;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceManager;
import android.support.v7.preference.XpPreferenceFragment;
import android.view.WindowManager;

import com.nowadays.mobile.R;
import com.nowadays.mobile.mvp.presenter.SettingPresenter;
import com.nowadays.mobile.mvp.presenter.SettingPresenterImpl;
import com.nowadays.mobile.mvp.view.SettingView;
import com.nowadays.mobile.utils.SettingInfo;

import net.xpece.android.support.preference.SeekBarDialogPreference;
import net.xpece.android.support.preference.SwitchPreference;

public class SettingFragment extends XpPreferenceFragment implements SettingView {
  private static final String Tag = SettingFragment.class.getSimpleName();

  private static SettingPresenter settingPresenter;

  @Override
  public void onCreatePreferences2(Bundle savedInstanceState, String rootKey) {
    addPreferencesFromResource(R.xml.preferences);

    settingPresenter = new SettingPresenterImpl(this);

    binPreferenceSummaryToValue(findPreference("setting_focus_time"));
    binPreferenceSummaryToValue(findPreference("setting_short_rest"));
    binPreferenceSummaryToValue(findPreference("setting_long_rest"));

    SwitchPreference preference = (SwitchPreference) findPreference("setting_prevent_lockscreen");
    preference.setOnPreferenceChangeListener(new LockScreenSwitchListener());
  }

  private static SharedPreferences.OnSharedPreferenceChangeListener sharedPreferenceChangeListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
      settingPresenter.updateFocus();
    }
  };

  private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {

    @Override
    public boolean onPreferenceChange(Preference preference, Object value) {

      if (preference instanceof SeekBarDialogPreference) {
        int intValue = (int) value;
        if (intValue == -1) {
          return false;
        }
        preference.setSummary(intValue + SettingInfo.STEP + preference.getContext().getString(R.string.minutes));
      }
      return true;
    }
  };

  private static void binPreferenceSummaryToValue(Preference preference) {
    preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
    if (preference instanceof SeekBarDialogPreference) {
      int value = PreferenceManager.getDefaultSharedPreferences(preference.getContext())
          .getInt(preference.getKey(), -1);
      sBindPreferenceSummaryToValueListener.onPreferenceChange(preference, value);
    }
  }

  private class LockScreenSwitchListener implements Preference.OnPreferenceChangeListener {
    @Override
    public boolean onPreferenceChange(Preference preference, Object value) {
      boolean isPrevent = (boolean) value;
      preventLockScreen(isPrevent);
      return true;
    }
  }

  /**
   * 根据设置是否阻止锁屏
   *
   * @param isPrevent 是否阻止锁屏
   */
  @Override
  public void preventLockScreen(boolean isPrevent) {
    if (isPrevent) {
      getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    } else {
      getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }
  }

  @Override
  public void onResume() {
    super.onResume();
    getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
    settingPresenter.onResume();
  }

  @Override
  public void onPause() {
    super.onPause();
    getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
    settingPresenter.onPause();
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
    settingPresenter.onDestroy();
  }
}
